axios.defaults.baseURL='http://localhost:8080'
//========================axios拦截器==
//前置拦截器
axios.interceptors.request.use(config=>{
    //携带token
    let uToken =  localStorage.getItem("token");
    if(uToken){
        config.headers['token']=uToken;
    }
    return config;//一定要返回配置信息
},error => {
    Promise.reject(error);
});

//后置拦截器
axios.interceptors.response.use(result=>{
    console.log(result.data+"jjjjjjj");
    let data = result.data;
    if(!data.success && data.message==="noLogin"){
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="login.html";
    }
    return result;
},error => {
    Promise.reject(error);
});
//========================axios拦截器===
//axios.defaults.baseURL="/api"//把axios配置为全局变量
Vue.prototype.$http=axios//给Vue这个类添加一个原型的属性,这个类的对象都能调用

//这个拦截，需要在所有页面都引入common.js
let url = location.href;
//如果不是登录页也不是注册页面，那么就要判断是否登录
if(url.indexOf("login.html")==-1 && url.indexOf("register.html")==-1 &&
    url.indexOf("callback.html")==-1 && url.indexOf("binder.html")==-1 && url.indexOf("index.html")==-1){
    let logininfo = localStorage.getItem("logininfo");
    if(!logininfo){//如果没登录，调到登录页
        location.href="login.html";
    }
}

/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}


